package com.knowledge.plus.http.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author PF.Tian
 * @since 2021/09/13
 */
@RestController
@Slf4j
@RequestMapping("http")
public class RestTemplateController {

    /**
     * post
     */
    @GetMapping("post")
    public void post_request() {
        String url = "http://localhost:8088/sgp/tcis/v1/enterprise/meter?accessToken=d3eb9a9233e52948740d7eb8c3062d14";
        JSONObject json = new JSONObject();
        json.put("meterCode", "123456789");
        json.put("imei", "123456789");
        json.put("iccid", "1");
        json.put("operator", "1");
        json.put("manufacturerCode", "1234");
        json.put("modelId", "1");
        json.put("meterDirect", "1");

        RestTemplate restTemplate = new RestTemplate();
        JSONObject responseBean = restTemplate.postForObject(url, json, JSONObject.class);
        System.out.println(responseBean);

        ResponseEntity<JSONObject> jsonObjectResponseEntity = restTemplate.postForEntity(url, json, JSONObject.class);
        System.out.println(jsonObjectResponseEntity);
    }

    /**
     * get
     */
    @GetMapping("get")
    public void get_request() {
        String url = "http://localhost:8088/sgp/tcis/v1/user/123456789/meter/cmd?taskId=21527ee117b54bec9c7cbd4b12b21e8b";

        RestTemplate restTemplate = new RestTemplate();
        JSONObject forObject = restTemplate.getForObject(url, JSONObject.class);
        System.out.println(forObject);

        ResponseEntity<JSONObject> forEntity = restTemplate.getForEntity(url, JSONObject.class);
        System.out.println(forEntity);
    }

    /**
     * put
     */
    @GetMapping("put")
    public void put_request() {
        String url = "http://localhost:8088/sgp/tcis/v1/enterprise/meter/123456789?accessToken=d3eb9a9233e52948740d7eb8c3062d14";
        JSONObject json = new JSONObject();
        json.put("meterCode", "123456789");
        json.put("manufacturerCode", "1");
        json.put("modelId", "1");
        json.put("meterType", "40027");
        json.put("meterDirect", "2");

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.put(url, json);

    }

    /**
     * delete
     */
    @GetMapping("delete")
    public void delete_request() {
        String url = "http://localhost:8088/sgp/tcis/v1/enterprise/meter/123456789?accessToken=d3eb9a9233e52948740d7eb8c3062d14";
        JSONObject json = new JSONObject();
        json.put("meterCode", "123456789");
        json.put("manufacturerCode", "1");
        json.put("modelId", "1");
        json.put("meterType", "40027");
        json.put("meterDirect", "2");

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.delete(url, json);

    }

    /**
     * 带Token的Get请求
     */
    public void getByToken() {
        String url = "http.www.baidu.com";
        Map<String, Object> param = new HashMap<>();
        param.put("param1", "1");
        param.put("param2", "2");

        HttpHeaders headers = new HttpHeaders();
        String token = "11111111111111111";
        headers.add("Authorization", token);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Map> result = restTemplate.exchange(
                url,
                HttpMethod.GET,
                new HttpEntity<String>(headers),
                Map.class,
                param
        );
        Map body = result.getBody();

    }

    /**
     * 带Token的Post请求
     */
    public void postByToken() {
        String postEntity = "null";
        String token = "1111111111";
        String url = "";

        MediaType type = MediaType.parseMediaType("application/json");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(type);
        headers.add("Accept", MediaType.ALL_VALUE);
        headers.add("Authorization", token);
        JSONObject param = JSONObject.parseObject(postEntity);
        HttpEntity<String> formEntity = new HttpEntity<String>(param.toString(), headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Integer> responsebody = restTemplate.exchange(
                url,
                HttpMethod.POST,
                formEntity,
                Integer.class);
        Integer contractId = responsebody.getBody();
    }

}
